<?php
class lang extends main{

	private static $message = array();
	private static $javascript = array();
	private static $defaultLang = array();
	
	protected static function init(){
		if(parent::getUrlVar(1) == "lang"){
			self::reloadLang();
		}else{
			//Default lang
			$sql = "SELECT * FROM ".parent::getConfig('db_prefix')."langs AS l WHERE l.active = 1 AND l.default = 1 LIMIT 1";
			$result = dao::request_assoc($sql);
			if($result){
				$lang = $result[0];
				self::$defaultLang["urlname"] = $lang['urlname'];
				self::$defaultLang["name"] = $lang['name'];
				self::$defaultLang["shortname"] = $lang['shortname'];
			}else{//Normalement c'est qu'en cas d'erreur de config BDD
				self::$defaultLang["urlname"] = parent::getConfig('lang');
				self::$defaultLang["name"] = parent::getConfig('lang');
				self::$defaultLang["shortname"] = parent::getConfig('lang');
			}
			
			if(!isset($_SESSION["config"]["lang"])){
				if(isset($_COOKIE["lang"]) && $_COOKIE['lang'] != ""){
					$_SESSION["config"]["lang"] = $_COOKIE['lang'];
					$_SESSION["config"]["langname"] = $_COOKIE['langname'];
					$_SESSION["config"]["langshortname"] = $_COOKIE['langshortname'];
				}else{
					$_SESSION["config"]["lang"] = self::$defaultLang['urlname'];
					$_SESSION["config"]["langname"] = self::$defaultLang['name'];
					$_SESSION["config"]["langshortname"] = self::$defaultLang['shortname'];
				}
			}
			
			$message = array();
			$javascript = array();
			
			if($_SESSION["config"]["lang"] != ""){
			    require_once(parent::getConfig('site_directory') .'lang/system/text/'.$_SESSION["config"]["lang"].'/common.lang');
			}else{
			    require_once(parent::getConfig('site_directory') .'lang/system/text/'. parent::getConfig('lang') .'/common.lang');
			}
			
			self::$message = $message;
		}
	}
	
	private static function reloadLang(){
		if(parent::getUrlVar(1) == "lang"){
			self::changeLang(parent::getUrlVar(2));
			parent::restoreLastUrlVars();
		}
		
		$message = array();
		$javascript = array();
		
		if($_SESSION["config"]["lang"] != ""){
		    require_once(parent::getConfig('site_directory') .'lang/system/text/'.$_SESSION["config"]["lang"].'/common.lang');
		}else{
		    require_once(parent::getConfig('site_directory') .'lang/system/text/'. parent::getConfig('lang') .'/common.lang');
		}
		
		self::$message = $message;
	}
	
	public static function show($intitule){
            return self::$message[$intitule];
	}
	
	public static function show_JS_all(){
		$js = '<script type="text/javascript">'."\n".
			'//<![CDATA['."\n".
			'var langJS = new Array;'."\n";
		foreach(self::$javascript as $lang_key => $tab_js){
			$js .= 'langJS[\''.$lang_key.'\'] = "'.$tab_js.'";'."\n";
		}
		
		$js .= '//]]>'."\n".
			'</script>';
			
		return $js;
	}
	
	public static function getTags(){
		return self::$message;
	}
	
	public static function getModTags($module){
		return self::$message[$module];
	}
	
	public static function getFormLang($type = "buttons", $img = true){
		
		switch($type){
			default:
			case "buttons":
				$langs = "<div class='lang_menu'>";
		
				
				$langdir = @opendir(main::getConfig('site_directory')."lang/system/text/");
				if($langdir != false){
					while ($f = readdir($langdir)) {
						if(is_dir(main::getConfig('site_directory')."lang/system/text/".$f) && $f != "." && $f != ".."){
							if(($_SESSION['config']['lang'] != $f) || (!isset($_SESSION['config']['lang']) && $f != parent::getConfig('lang')))
								$javascript = 'rel="'.$f.'" onclick="javascript:admin_switchlang(this);"';
							else    $javascript = "";
							
							if($img)
								$langs .= "<div class='lang_flag' ".$javascript."><img src='". parent::getConfig('site_url') ."system/lang/".$f.".png' alt='".ucfirst($f)."'/></div>";
							else
								$langs .= "<div class='lang_flag' ".$javascript.">".ucfirst($f)."</div>";
						}
					}
					closedir($langdir);
				}
				
				$langs .= "</div>";
				break;
			
			case "select":
				
				break;
			
			case "array":
				
				break;
			
			case "array_only_available":
				
				break;
			
			case "system":
				$sql = "SELECT * FROM ".parent::getConfig('db_prefix')."langs AS l WHERE l.active = 1";
				$result = dao::request_assoc($sql);
				
				$langs  = "<div class='lang_menu'>";
				$langs .= "	<div class='current_lang' style='background-image:URL(\"/system/lang/".$_SESSION['config']['lang'].".png\");'>".$_SESSION['config']['langshortname']."</div>";
				$langs .= "	<div class='select_lang'>";
				$langs .= "		<input type='button' id='btn_changelang' onmouseover='javascript:admin_changelang(true);' onmouseout='javascript:admin_changelang(false);' value='".lang::show('Change')."'/>";
				$langs .= "			<div id='changelang_box'>";
				if($result){
					foreach($result as $lang){
						$langs .= "		<div class='lang_option' rel='".$lang['id']."' style='background-image:URL(\"".main::getConfig('site_url')."system/lang/".$lang['urlname'].".png\");padding-left:18px;' onclick='javascript:admin_switchlang(this);' onmouseout='javascript:admin_changelang(false);' onmouseover='javascript:admin_changelang(true);'>".$lang['name']."</div>";
					}
				}
				$langs .= "			</div>";
				$langs .= "	</div>";
				$langs .= "</div>";
				break;
		}
		
		return $langs;
	}
	
	public static function getDefaultLang(){
		return self::$defaultLang['urlname'];
	}
	
	public static function changeLang($lang){
		$sql = "SELECT * FROM ".parent::getConfig('db_prefix')."langs AS l WHERE l.active = 1 AND (l.id = ".intval($lang)." OR l.urlname='".$lang."')";
		$result = dao::request_assoc($sql);
		if(count($result)){
			$lang = $result[0];
			self::$defaultLang["urlname"] = $lang['urlname'];
			self::$defaultLang["name"] = $lang['name'];
			self::$defaultLang["shortname"] = $lang['shortname'];
		}else{//Normalement c'est qu'en cas d'erreur de config BDD
			self::$defaultLang["urlname"] = parent::getConfig('lang');
			self::$defaultLang["name"] = parent::getConfig('lang');
			self::$defaultLang["shortname"] = parent::getConfig('lang');
		}
		
		//Session
		$_SESSION["config"]["lang"] = self::$defaultLang['urlname'];
		$_SESSION["config"]["langname"] = self::$defaultLang['name'];
		$_SESSION["config"]["langshortname"] = self::$defaultLang['shortname'];
		//Cookie
		@setcookie('lang', self::$defaultLang['urlname'], time() + 365*24*3600, null, null, false, true);
		@setcookie('langname', self::$defaultLang['name'], time() + 365*24*3600, null, null, false, true);
		@setcookie('langshortname', self::$defaultLang['shortname'], time() + 365*24*3600, null, null, false, true);
	}
	
	public static function addModLang($module){
		$folder = parent::getConfig('site_directory')."lang/".$module."/text/".$_SESSION["config"]["lang"]."/";
		$langdir = @opendir($folder);
		if($langdir){
			while ($f = readdir($langdir)) {
				if((is_file($folder.$f))&&($folder.$f!=$folder."thumbs.db")&&($folder.$f!=$folder."index.php")&&(preg_match('#\.lang#',$f)))
				{
				    require_once($folder.$f);
				    $modLang = $message;
				}
			}
			closedir($langdir);
		}
		self::$message[$module] = $modLang;
	}
	
	public static function showModLang($module,$var){
		$sentence = self::$message[$module][$var];
		return $sentence;
	}
	
	public static function getModLangs($module){
		$folder = parent::getConfig('site_directory')."lang/".$module."/text/";
		$langdir = opendir($folder);
		while ($f = readdir($langdir)){
			if((is_dir($folder.$f))&&($f != ".")&&($f != "..")){
				$tab_lang[] = $f;
			}
		}
		
		return $tab_lang;
	}
	
	public static function getSentenceInOtherLang($lang,$var){
		if(file_exists(parent::getConfig('site_directory') .'lang/system/text/'. $lang .'/common.lang')){
			require_once(parent::getConfig('site_directory') .'lang/system/text/'. parent::getConfig('lang') .'/common.lang');
			if($message[$var] != ""){
				return $message[$var];
			}else{
				return false;
			}
		}else{
			return false;
		}
	}
	
	public static function getSentenceInOtherModLang($lang,$var,$module){
		$folder = parent::getConfig('site_directory')."lang/".$module."/text/".$lang."/";
		$langdir = @opendir($folder);
		if($langdir){
			$message = array();
			while ($f = readdir($langdir)) {
				if((is_file($folder.$f))&&($folder.$f!=$folder."thumbs.db")&&($folder.$f!=$folder."index.php")&&(preg_match('#\.lang#',$f))){
				    require_once($folder.$f);
				    $modLang = $message;
				}
			}
			closedir($langdir);
			
			if($modLang[$var] != ""){
				return $modLang[$var];
			}else{
				return false;
			}
		}else{
			return false;
		}
	}
}
?>